package com.filetransfer.utils;
public class StringFormat {
    private static final String[] UNITS = {"B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
    private static final long UNIT_SIZE = 1024;
    // 把字节数值转换成可读的文本
    // 如 102400 -> 100.0 KB
    public static String formatSize(long size) {
        if (size <= 0) {
            return "0 B";
        }
        int unitIndex = 0;
        double displaySize = size;
        // 计算适当的单位
        while (displaySize >= UNIT_SIZE && unitIndex < UNITS.length - 1) {
            displaySize /= UNIT_SIZE;
            unitIndex++;
        }
        // 格式化为字符串，保留一位小数
        return String.format("%.1f %s", displaySize, UNITS[unitIndex]);
    }
}
